package org.vacoor.ticket.bean.param.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.vacoor.ticket.bean.Passenger;
import org.vacoor.ticket.bean.param.Parameter;

public class OrderConfirmQuery__ implements Parameter{
	private String randCode;
//	private String tFlag;
	private List<Passenger> passengers = new ArrayList<Passenger>();
	private OrderParam order ;
	
	public OrderConfirmQuery__(OrderParam order,List<Passenger> passengers,String randCode){
		this.order = order;
		this.passengers = passengers;
		this.randCode = randCode;
	}

	public void addPassenger(Passenger p) {
		passengers.add(p);
	}
	
	public Passenger removePassenger(int index) {
		return passengers.remove(index);
	}
	
	public void setTicketOrder(OrderParam order) {
		this.order = order;
	}
	
	public OrderParam getTicketOrder() {
		return order;
	}
	
	@Override
	public String getQueryString() {
		return getOrderQueryString() + "&" +
				getPassengerQueryString() +
				//这个好像也不是必须的
				"&checkbox9=Y" + 
				"&checkbox0=0";
	}

	private String getPassengerQueryString() {
		//这些有些是可以不要的,没测试
		StringBuilder sb = new StringBuilder();
		if(passengers.size() > 0 ) {
			//这个是表示从历史中选择的人,不写没测试
			Passenger p = passengers.get(0);
			sb.append("oldPassengers=" + p.getName() + "," + p.getCardType().getValue() + "," + p.getCardNo());
		}
		for(int i=0; i<passengers.size();i++) {
			Passenger p = passengers.get(i);
			sb.append("&passenger_" + (i+1) + "_seat=" + p.getSeatType().getValue());
			sb.append("&passenger_" + (i+1) + "_ticket=" + p.getTicketType().getValue());
			sb.append("&passenger_" + (i+1) + "_name=" + p.getName());
			sb.append("&passenger_" + (i+1) + "_cardtype=" + p.getCardType().getValue());
			sb.append("&passenger_" + (i+1) + "_cardno=" + p.getCardNo());
			sb.append("&passenger_" + (i+1) + "_mobileno=" + p.getMobile());
			//这个是必须的
			sb.append("&passengerTickets=" + 
					p.getSeatType().getValue() + "," +
					"undefined," +
					p.getTicketType().getValue() + "," +
					p.getName() + "," +
					p.getCardType().getValue() + "," +
					p.getCardNo() + "," +
					p.getMobile() + "," +
					(p.isSavePassenger() ? "Y" : "N"));
		}
		return sb.toString();
	}
	
	private String getOrderQueryString() {
		return 
				//
		"org.apache.struts.taglib.html.TOKEN=" + order.getToken() + 	//TOKEN
		"&leftTicketStr=" + order.getRemainTicketsDetail() +		//订单号
		"&orderRequest.train_date=" + new SimpleDateFormat("yyyy-MM-dd").format(order.getTrainDate())+ 
		"&orderRequest.train_no=" + order.getTrainId() +	//列车编号
		"&orderRequest.station_train_code=" + order.getTrainNo()	+ //车次
		"&orderRequest.from_station_name=" + order.getFromStation().getName() +
		"&orderRequest.to_station_name=" + order.getArriveStation().getName() + 
		"&orderRequest.from_station_telecode=" + order.getFromStation().getCode() +
		"&orderRequest.to_station_telecode=" + order.getArriveStation().getCode() + 
		"&orderRequest.start_time=" + order.getStartTime() +
		"&orderRequest.end_time=" + order.getEndTime() +
		"&orderRequest.bed_level_order_num=" + order.getBedLevelOrderNum() +
		"&orderRequest.cancel_flag=" + order.getCancelFlag() +
		"&orderRequest.ticket_type_order_num=" + order.getTicketTypeOrderNum() +  
		"&orderRequest.seat_type_code=" + order.getSeatTypeCode() +
		"&orderRequest.id_mode=" + order.getIdMode() +
		"&orderRequest.reserve_flag=" + order.getReserveFlag() +
		"&randCode=" + randCode + 
		"&rand=" + randCode;
	}

	public String getRandCode() {
		return randCode;
	}

	public void setRandCode(String randCode) {
		this.randCode = randCode;
	}
	
	
}
